package com.ai.trickycards.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ai.trickycards.BuildConfig
import com.ai.trickycards.Constant
import com.ai.trickycards.R
import com.ai.trickycards.databinding.ActivitySettingBinding
import com.ai.trickycards.utils.getString
import com.ai.trickycards.utils.setString


@SuppressLint("SetTextI18n")
class SettingActivity : BaseActivity() {

    private val binding by lazy {
        ActivitySettingBinding.inflate(layoutInflater)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setWindowInsets(binding.root)

        binding.ivClose.setOnClickListener { finish() }
        binding.tvVersion.text = "v${BuildConfig.VERSION_NAME}"
        binding.etNick.setString(appViewModel.userInfoFlow.value.userName)

        binding.tvPrivacyPolicy.setOnClickListener {
            WebActivity.open(this@SettingActivity, Constant.H5.PRIVACY_POLICY, binding.tvPrivacyPolicyLabel.getString())
        }

        binding.tvTermsOfUse.setOnClickListener {
            WebActivity.open(this@SettingActivity, Constant.H5.TERMS_POLICY, binding.tvTermsOfUseLabel.getString())
        }
    }

    override fun finish() {
        super.finish()
        val newNick = binding.etNick.getString()
        if (newNick.isNotEmpty() && newNick.isNotBlank()) {
            val userInfo = appViewModel.userInfoFlow.value.copy(userName = newNick)
            appViewModel.changeUserInfo(userInfo)
        }
    }
}